//
//  ArrayImplementaionView.swift
//  DataStructure
//
//  Created by kys-20 on 2019/8/26.
//  Copyright © 2019 kys-20. All rights reserved.
//

import Foundation
import UIKit
class ArrayImplementaionView: UIView {
    
    let getTextFied = UITextField()
    let pushButton = UIButton()
    let popButton = UIButton()
    let clearButton = UIButton()
    let topLable = UILabel()
    let topSumLable = UILabel()
    let buleLable = UILabel()
    var lableArray = [UILabel]()
    var lableArrayA = [UILabel]()
   let slider = UISlider()//滑块 用来控制时间
    var topNum = 0
    var timer:Timer!
    var timerPop:Timer!
    var ti = 0
    var isturn = false
    var sumTime = 1.00
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUpUI()
    }
    func setUpUI(){
        
        slider.frame = CGRect(x: 10, y: UIScreen.main.bounds.height-60, width: UIScreen.main.bounds.width-20, height: 30)
             slider.minimumValue = 0
             slider.maximumValue = 4
             slider.value = 1.00
             self.addSubview(slider)
             slider.isContinuous = false//滑块滑动停止后才触发ValueChanged事件
             slider.addTarget(self, action: #selector(ActionSliderValueChange), for: .valueChanged)
        
        getTextFied.frame = CGRect(x: 10, y: 100, width: 60, height: 30)
        pushButton.frame = CGRect(x: 75, y: 100, width: 50, height: 30)
        popButton.frame = CGRect(x: 130, y: 100, width: 40, height: 30)
        clearButton.frame = CGRect(x: 175, y: 100, width: 50, height: 30)
        getTextFied.layer.masksToBounds = true
        getTextFied.layer.borderWidth = 1
        getTextFied.layer.borderColor = UIColor.red.cgColor
        self.addSubview(getTextFied)
        pushButton.setTitle("Push", for: .normal)
        pushButton.layer.borderWidth = 1
        pushButton.layer.borderColor = UIColor.black.cgColor
        pushButton.setTitleColor(UIColor.black, for: .normal)
        pushButton.setTitleColor(UIColor.brown, for: .highlighted)
        pushButton.addTarget(self, action: #selector(pushButtonAction), for: .touchUpInside)
        self.addSubview(pushButton)
        popButton.setTitle("Pop", for: .normal)
        popButton.layer.borderWidth = 1
        popButton.layer.borderColor = UIColor.black.cgColor
        popButton.setTitleColor(UIColor.black, for: .normal)
        popButton.setTitleColor(UIColor.brown, for: .highlighted)
        popButton.addTarget(self, action: #selector(popButtonAction), for: .touchUpInside)
        self.addSubview(popButton)
        clearButton.setTitle("clear", for: .normal)
        clearButton.layer.borderWidth = 1
        clearButton.layer.borderColor = UIColor.black.cgColor
        clearButton.setTitleColor(UIColor.black, for: .normal)
        clearButton.setTitleColor(UIColor.brown, for: .highlighted)
        clearButton.addTarget(self, action: #selector(clearButtonAction), for: .touchUpInside)
        self.addSubview(clearButton)
        
        topLable.frame = CGRect(x: 30, y: 150, width: 50, height: 50)
        topLable.text = "top"
        topLable.textColor = UIColor.black
        self.addSubview(topLable)
        
        topSumLable.frame = CGRect(x: 80, y: 150, width: 50, height: 50)
        topSumLable.textAlignment = .center
        topSumLable.text = String(topNum)
        topSumLable.layer.masksToBounds = true
        topSumLable.layer.borderColor = UIColor.black.cgColor
        topSumLable.layer.borderWidth = 1
        self.addSubview(topSumLable)
        buleLable.frame = CGRect(x: 80, y: 150, width: 50, height: 50)
        buleLable.layer.masksToBounds = true
        buleLable.layer.cornerRadius = 25
        buleLable.layer.borderColor = UIColor.blue.cgColor
        buleLable.isHidden = true
        buleLable.layer.borderWidth = 1
        self.addSubview(buleLable)
        
        
        let lableJiange:CGFloat = 10
        for i in 0...6{
            let la = UILabel()
            la.frame = CGRect(x:lableJiange + CGFloat(i)*50 , y: 320, width: 50, height: 50)
            la.layer.masksToBounds = true
            la.textAlignment = .center
            la.layer.borderColor = UIColor.black.cgColor
            la.layer.borderWidth = 1
            self.addSubview(la)
            lableArray.append(la)
        }
        for i in 0...6{
            let la = UILabel()
            la.text = String(i)
            la.textAlignment = .center
            la.frame = CGRect(x:lableJiange + CGFloat(i)*50 , y: 370, width: 50, height: 50)
            self.addSubview(la)
            lableArrayA.append(la)
        }
        
    }
    @objc func ActionSliderValueChange(){
           if Double(slider.value) <= 0.0001{
               sumTime = 0.0001
           }else{
               sumTime = Double(slider.value)
           }
           
       }
    
    @objc func clearButtonAction(){
       
        topNum = 0
        topSumLable.text = String(topNum)
        for i in lableArray{
            i.text = ""
        }
    }
    @objc func pushButtonAction(){
       
        if getTextFied.text! != ""{
             buttonYes()
            if topNum <= 6{
                buttonNo()
                UIView.animate(withDuration: sumTime, animations: {
                    self.buleLable.isHidden = false
                    self.buleLable.center.x = self.lableArrayA[self.topNum].center.x
                    self.buleLable.center.y = self.lableArrayA[self.topNum].center.y
                    self.lableArrayA[self.topNum].text = String(self.topNum)
                }) { (_) in
                    self.buleLable.isHidden = true
                    self.buleLable.center.x = self.topSumLable.center.x
                    self.buleLable.center.y = self.topSumLable.center.y
                    self.lableArray[self.topNum].text = self.getTextFied.text
                    self.topNum = self.topNum + 1
                    self.changeBound()
                }
            }
            
        }
        
    }
    func changeBound(){
        timer = Timer.scheduledTimer(timeInterval: sumTime*0.1,target:self,selector:#selector(tickDown),userInfo:nil,repeats:true)
    }
    @objc func tickDown()
    { self.topSumLable.layer.borderColor = UIColor.red.cgColor
        self.topSumLable.layer.borderWidth += 1
        ti += 1
        aa()
        
    }
    func aa(){
        if ti == 4{
            timer.invalidate()
            self.topSumLable.text = String(self.topNum)
            self.topSumLable.layer.borderColor = UIColor.black.cgColor
            self.topSumLable.layer.borderWidth = 1
            getTextFied.text = ""
            ti = 0
            buttonYes()
        }
    }
    @objc func popButtonAction(){
        if topNum>0 {
            
            buttonNo()
            changeBoundPop()
        }
    }
    func changeBoundPop(){
        timerPop = Timer.scheduledTimer(timeInterval: sumTime*0.1,target:self,selector:#selector(tickDownPop),userInfo:nil,repeats:true)
    }
    @objc func tickDownPop(){
        
        self.topSumLable.layer.borderColor = UIColor.red.cgColor
        self.topSumLable.layer.borderWidth += 1
        ti += 1
        bb()
    }
    func bb(){
        if ti == 4{
            bbb()
            timerPop.invalidate()
            self.topSumLable.layer.borderColor = UIColor.black.cgColor
            self.topSumLable.layer.borderWidth = 1
            
            ti = 0
            
        }
    }
    func bbb(){
        UIView.animate(withDuration: sumTime, animations: {
            self.buleLable.isHidden = false
            self.topNum = self.topNum - 1
            self.topSumLable.text = String(self.topNum)
            self.buleLable.center.x = self.lableArrayA[self.topNum].center.x
            self.buleLable.center.y = self.lableArrayA[self.topNum].center.y
            //                    self.lableArrayA[self.topNum].text = String(self.topNum)
        }) { (_) in
            self.buleLable.isHidden = true
            self.buleLable.center.x = self.topSumLable.center.x
            self.buleLable.center.y = self.topSumLable.center.y
            self.lableArray[self.topNum].text = ""
            self.buttonYes()
            
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func buttonYes(){
        pushButton.isEnabled = true
        popButton.isEnabled = true
        clearButton.isEnabled = true
        slider.isEnabled = true
        
    }
    func buttonNo(){
        pushButton.isEnabled = false
        popButton.isEnabled = false
        clearButton.isEnabled = false
        slider.isEnabled = false
    }
}
